package org.nightstudio.common.util.cookie;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nightstudio.common.util.constant.CookieConstant;

public class CookieUtil {
	public static void addCookie(HttpServletResponse response, String name,
			String value) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath(CookieConstant.COOKIE_PATH);
		cookie.setMaxAge(CookieConstant.MAX_AGE);
		response.addCookie(cookie);
	}

	public static void removeCookie(HttpServletResponse response, String name) {
		Cookie cookie = new Cookie(name, "");
		cookie.setPath(CookieConstant.COOKIE_PATH);
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}
	
	public static String getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (name.equals(c.getName())) {
					return c.getValue();
				}
			}
		}
		return null;
	}
}
